<template>
<div>
        <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>音乐管理</el-breadcrumb-item>
    <el-breadcrumb-item>音乐列表</el-breadcrumb-item>
    </el-breadcrumb>

        <!-- 卡片区域 -->
    <el-card>
        <el-row :gutter="20">
            <el-col :span="8">
                    <el-input placeholder="请输入音乐名称" v-model="queryInfo.query" clearable="">
                    <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
                </el-input>
            </el-col>
                <el-button type="primary"  @click="addDialogVisible = true">添加音乐</el-button>
            <el-col :span="4"></el-col>
        </el-row>


            <!-- 表格区域 -->
            <el-table  :data="musiclist" border stripe>
                <el-table-column label="序号" prop="id" width="50px">
                </el-table-column>

                <el-table-column label="音乐名称" prop="name" width="150px">
                </el-table-column>

                <el-table-column label="音乐cdn地址" prop="src" />
                
               

                <el-table-column label="操作" width="130px">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="editIt(scope.row.name,scope.row.src,scope.row.id)"></el-button>
                        <el-button type="danger" icon="el-icon-delete" 
                         @click="removeById(scope.row.id)" size="mini"></el-button>
                    </template>
                </el-table-column>

            </el-table>  

              <!-- 分页区域         -->
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page=" queryInfo.pagenum"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total" background>
                </el-pagination>       
    </el-card>
                <!-- 添加音乐的提示框 -->
                <el-dialog
                title="添加用户"
                :visible.sync="addDialogVisible"
                width="50%" @close="addDialogClosed">
                <!-- 内容主体区域 -->
               <el-form ref="addFormRef" :model="addForm" :rules="addFormRules" label-width="95px">
                <el-form-item label="音乐名称" prop="name">
                    <el-input v-model="addForm.name"></el-input>
                </el-form-item>
                 <el-form-item label="音乐地址" prop="src">
                    <el-input v-model="addForm.src"></el-input>
                </el-form-item>
               </el-form>
                <!-- 底部区域 -->
                <span slot="footer" class="dialog-footer">
                    <el-button @click="addDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="addMusic">确 定</el-button>
                </span>
                </el-dialog>



                <!-- 修改音乐的提示框 -->
                <el-dialog
                title="修改用户"
                :visible.sync="editDialogVisible"
                width="50%" @close="addDialogClosed">
                <!-- 内容主体区域 -->
               <el-form ref="addFormRef" :model="addForm" :rules="addFormRules" label-width="95px">
                <el-form-item label="音乐名称" prop="name">
                    <el-input v-model="addForm.name"></el-input>
                </el-form-item>
                 <el-form-item label="音乐地址" prop="src">
                    <el-input v-model="addForm.src"></el-input>
                </el-form-item>
               </el-form>
                <!-- 底部区域 -->
                <span slot="footer" class="dialog-footer">
                    <el-button @click="editDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="editMusic">确 定</el-button>
                </span>
                </el-dialog>



</div>
</template>

<script>
export default {
   data(){
    return{
      queryInfo: {
          // query:"",
          id:"",
          pagenum:1,
          pagesize:10
      },
      musiclist:[],
      total:0,
      addDialogVisible:false,
      editDialogVisible:false,
      addForm:{
          name:"",
          src:""
      },
      addFormRules:{
          name:[
              {required:true,message:"请输入音乐名称",trigger:"blur"},
              {min:1,max:100,message:"音乐名称长度在1-100个字符之间",trigger:"blur"}
          ],
          src:[
              {required:true,message:"请输入音乐cdn地址",trigger:"blur"},
              {min:1,max:1000,message:"音乐地址长度在1-100个字符之间",trigger:"blur"}
          ]
      }
  }
},
   methods:{
       async getGoodsList(){
          const {data:res} = await this.$http.get('music/lists',{ params:this.queryInfo})
          this.musiclist = res.data
         
          
          this.total = res.total;
        // console.log(res.data);
          this.$message.success('获取音乐成功')
        
       },
       handleSizeChange(newSize){
        this.queryInfo.pagesize = newSize
        this.getGoodsList()
       },
       handleCurrentChange(newPage){
         this.queryInfo.pagenum =  newPage  
         this.getGoodsList()
       },
       async removeById(id){
          const confirmResult = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)

        if(confirmResult !== 'confirm'){
            return this.$message.info('已经取消删除')
        }
        // console.log(id);
        const res = await this.$http.get("music/drop/"+id)
        // console.log(res.data);
        
        if(res.data !==1) return this.$message.error('删除失败');
        this.$message.success('删除成功')
        this.getGoodsList()
       },
    //    监听添加用户的对话框事件
       addDialogClosed(){
        this.$refs.addFormRef.resetFields();
       },
       addMusic(){
       this.$refs.addFormRef.validate(async valid =>{
            if(!valid)return;
            const res = await this.$http.get('music/add',{ params:this.addForm})
            if(res.data==1) this.$message.error('添加失败');
            this.$message.success('添加成功')
             this.addDialogVisible = false 
             this.getGoodsList()  
        });
      },
       editIt(name,src,id){
        this.editDialogVisible = true 
       
        this.addForm.name = name; 
        this.addForm.src = src; 
        this.addForm.id = id; 
       
      },
      async editMusic(){
           this.$refs.addFormRef.validate(async valid =>{
            if(!valid)return;
           const res = await this.$http.get('music/edit',{ params:this.addForm})
        if(res.data!==1) this.$message.error('更新失败');
        this.$message.success('更新成功')
        
      
        this.editDialogVisible = false 
        this.getGoodsList()  
           })
      }


},
created(){
    this.getGoodsList();
}
}
</script>

<style scoped>

</style>